package com.fingard.dsp.web.page;

import com.fingard.FGBiz;
import com.fingard.diagnostics.LogHelper;
import com.fingard.dsp.bank.dbobj.receipt.ReceiptDbHelper;
import com.fingard.dsp.bank.dbobj.receipt.ReceiptMapping;
import com.fingard.dsp.bank.directConfig.FileSaveSet;
import com.fingard.dsp.interact.FileServerInvokerBase;
import com.fingard.dsp.interact.GenerateUrlReq;
import com.fingard.dsp.interact.GenerateUrlResp;
import com.fingard.dsp.interact.IFileServerInvoker;
import com.fingard.text.StringHelper;

public class ReceiptUrl extends PageBase {

	@Override
	public void pageLoad() throws Exception{
		
		httpContext.response.setContentType("json", httpContext.request.webBrowser);
		String rHashcode= "";
		String rUrl = "";
		
		ReceiptMapping rcptQryMp = ReceiptDetail.getMapByQryStr(httpContext.request);
		ReceiptMapping rcptMp = ReceiptDbHelper.getRcptMpByAny(rcptQryMp);
		if(rcptMp!=null){
			rHashcode = rcptMp.customFileId;
			FileSaveSet fileSet = FGBiz.dspCenter.dspConfig.getFileSaveSet("receipt");
			String cmpSaveType = "";
			if(StringHelper.hasAnyChar(fileSet.saveType)){
				cmpSaveType = fileSet.saveType.toLowerCase();
			}
			if("custom.fileserver".equals(cmpSaveType) || "oss".equals(cmpSaveType)){
				String fileId = rcptMp.customFileId;
				try {
                    String className = "";
                    if("custom.fileserver".equals(cmpSaveType)){
                    	className = "com.fingard.dsp.interact.custom." + FGBiz.dspCenter.dspSet.customCode.toLowerCase() + "." + "FileServerInvoker";
                    }else{
                    	className = "com.fingard.dsp.interact.oss." + fileSet.ossSet.ossType.toLowerCase() + "." + "FileServerInvoker";
                    }
                    Class<?> fsClass = Class.forName(className);
                    if (fsClass != null) {
                    	FileServerInvokerBase fsClassBase = (FileServerInvokerBase) fsClass.newInstance();
                    	fsClassBase.fsSet = fileSet;
                    	IFileServerInvoker iFsInvoker = (IFileServerInvoker) fsClassBase;
                    	GenerateUrlReq genReq = new GenerateUrlReq();
                    	genReq.fileId = fileId;
                    	GenerateUrlResp genResp = iFsInvoker.generateUrl(genReq);
                    	if(genResp.isCompleted){
                    		rUrl = genResp.resultUrl;
                    	}else{
                    		rUrl = genResp.sbMsg.toString();
                    	}
                    }else{
                    	rUrl = "不存在类"+className;
                    }
                }catch (Throwable t) {
                	//resp.connState = ConnState.CLASS_NOT_FOUND;
                	rUrl = LogHelper.getStackTrace(t);
                	FGBiz.limitMsg.exception("ReceiptUrl", t);
                }
			}else{
				rUrl = "不支持生成Url的回单保存方式，仅支持custom.fileserver和oss";
			}
		}else{
			rHashcode = "-1";
			rUrl = "未能找到该回单";
		}
		//sbJson.append("\"urid\":\""+eachFront.urid.trim()+"\"");
		httpContext.response.respMsg = "{\"hashcode\":\""+rHashcode+"\",\"url\":\""+rUrl+"\"}";
	}
}
